use criterion::{criterion_group, criterion_main, Criterion};
use bloom_filter::bloom_filter::{BloomFilter, HashFunc};

// 定义一个简单的哈希函数用于测试
fn simple_hash(data: &u32) -> u32 {
    *data
}

fn bloom_filter_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("bloom_filter");

    // 测试 BloomFilter::new
    group.bench_function("bloom_filter_new", |b| {
        b.iter(|| {
            BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        });
    });

    // 测试 BloomFilter::insert
    group.bench_function("bloom_filter_insert", |b| {
        let mut filter = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        b.iter(|| {
            filter.insert(&42);
        });
    });

    // 测试 BloomFilter::query
    group.bench_function("bloom_filter_query", |b| {
        let mut filter = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        filter.insert(&42);
        b.iter(|| {
            filter.query(&42);
        });
    });

    // 测试 BloomFilter::read
    group.bench_function("bloom_filter_read", |b| {
        let filter = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        let mut buffer = vec![0u8; (1000 + 7) / 8];
        b.iter(|| {
            filter.read(&mut buffer).unwrap();
        });
    });

    // 测试 BloomFilter::load
    group.bench_function("bloom_filter_load", |b| {
        let mut filter = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        let source = vec![0u8; (1000 + 7) / 8];
        b.iter(|| {
            filter.load(&source).unwrap();
        });
    });

    // 测试 BloomFilter::union
    group.bench_function("bloom_filter_union", |b| {
        let filter1 = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        let filter2 = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        b.iter(|| {
            BloomFilter::union(&filter1, &filter2).unwrap();
        });
    });

    // 测试 BloomFilter::intersection
    group.bench_function("bloom_filter_intersection", |b| {
        let filter1 = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        let filter2 = BloomFilter::new(1000, simple_hash as HashFunc<u32>, 3).unwrap();
        b.iter(|| {
            BloomFilter::intersection(&filter1, &filter2).unwrap();
        });
    });

    group.finish();
}

criterion_group!(benches, bloom_filter_benchmark);
criterion_main!(benches);